package com.hjm.project.salaryPlan.agentActivityDetails.service.impl;

import java.util.List;

import com.hjm.common.exception.BusinessException;
import com.hjm.common.utils.DateUtils;
import com.hjm.common.utils.security.ShiroUtils;
import com.hjm.project.salaryPlan.agentActivityOpen.mapper.AgentActivityOpenMapper;
import org.springframework.stereotype.Service;
import com.hjm.project.salaryPlan.agentActivityDetails.mapper.AgentActivityDetailsMapper;
import com.hjm.project.salaryPlan.agentActivityDetails.domain.AgentActivityDetails;
import com.hjm.project.salaryPlan.agentActivityDetails.service.IAgentActivityDetailsService;
import com.hjm.common.utils.text.Convert;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 活动参与人员明细Service业务层处理
 * 
 * @author maker-platform
 * @date 2022-04-07
 */
@Service
public class AgentActivityDetailsServiceImpl implements IAgentActivityDetailsService {
    @Resource
    private AgentActivityDetailsMapper agentActivityDetailsMapper;

    @Resource
    private AgentActivityOpenMapper agentActivityOpenMapper;

    /**
     * 查询活动参与人员明细
     * 
     * @param id 活动参与人员明细ID
     * @return 活动参与人员明细
     */
    @Override
    public AgentActivityDetails selectAgentActivityDetailsById(String id) {
        return agentActivityDetailsMapper.selectAgentActivityDetailsById(id);
    }

    /**
     * 查询活动参与人员明细列表
     * 
     * @param agentActivityDetails 活动参与人员明细
     * @return 活动参与人员明细
     */
    @Override
    public List<AgentActivityDetails> selectAgentActivityDetailsList(AgentActivityDetails agentActivityDetails) {
        return agentActivityDetailsMapper.selectAgentActivityDetailsList(agentActivityDetails);
    }

    /**
     * 查询活动参与人员明细列表
     *
     * @param agentActivityDetails 活动参与人员明细
     * @return 活动参与人员明细 一个结果
     */
    @Override
    public AgentActivityDetails selectAgentActivityDetailsOne(AgentActivityDetails agentActivityDetails) {
        return agentActivityDetailsMapper.selectAgentActivityDetailsOne(agentActivityDetails);
    }

    /**
     * 新增活动参与人员明细
     * 
     * @param agentActivityDetails 活动参与人员明细
     * @return 结果
     */
    @Override
    public int insertAgentActivityDetails(AgentActivityDetails agentActivityDetails) {

        Integer startGrade = Integer.parseInt(agentActivityDetails.getStartGrade());
        Integer endGrade = Integer.parseInt(agentActivityDetails.getEndGrade());
        if (startGrade > endGrade) {
            throw new BusinessException("起始等级不可高于结束等级");
        }
        StringBuffer sb = new StringBuffer();
        for (int i = startGrade; i <= endGrade; i++) {
            sb.append("V" + i).append(",");
        }
        agentActivityDetails.setLevelScope(sb.deleteCharAt(sb.length()-1).toString());
        agentActivityDetails.setCreateMan(ShiroUtils.getLoginName());
        agentActivityDetails.setCreateDate(DateUtils.getDate());
        return agentActivityDetailsMapper.insertAgentActivityDetails(agentActivityDetails);
    }

    /**
     * 修改活动参与人员明细
     * 
     * @param agentActivityDetails 活动参与人员明细
     * @return 结果
     */
    @Override
    public int updateAgentActivityDetails(AgentActivityDetails agentActivityDetails) {
        Integer startGrade = Integer.parseInt(agentActivityDetails.getStartGrade());
        Integer endGrade = Integer.parseInt(agentActivityDetails.getEndGrade());
        if (startGrade > endGrade) {
            throw new BusinessException("起始等级不可高于结束等级");
        }
        StringBuffer sb = new StringBuffer();
        for (int i = startGrade; i <= endGrade; i++) {
            sb.append("V" + i).append(",");
        }
        agentActivityDetails.setLevelScope(sb.deleteCharAt(sb.length()-1).toString());
        return agentActivityDetailsMapper.updateAgentActivityDetails(agentActivityDetails);
    }

    /**
     * 删除活动参与人员明细对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteAgentActivityDetailsByIds(String ids) {
        return agentActivityDetailsMapper.deleteAgentActivityDetailsByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除活动参与人员明细信息
     * 
     * @param agentNum 活动参与人员明细ID
     * @return 结果
     */
    @Override
    public int deleteAgentActivityDetailsById(String agentNum) {
        return agentActivityDetailsMapper.deleteAgentActivityDetailsById(agentNum);
    }

    /**
     * 活动送审
     *
     * @param id
     * @Param [ids]
     * @Return int
     */
    @Override
    @Transactional
    public int sendAudit(String id) {
        AgentActivityDetails agentActivityDetails = agentActivityDetailsMapper.selectAgentActivityDetailsById(id);
        if (1 == agentActivityDetails.getAuditStatus()) {
            throw new BusinessException("不可重复上送");
        } else if (2 == agentActivityDetails.getAuditStatus()) {
            throw new BusinessException("审核已通过,不可重复上送");
        }

        AgentActivityDetails details = new AgentActivityDetails();
        details.setId(Integer.parseInt(id));
        details.setAuditStatus(1);
        int i = agentActivityDetailsMapper.updateAgentActivityDetails(details);
        return i;
    }

    /**
     * 审核活动参与人员
     *
     * @param agentActivityDetails
     * @Param [ids]
     * @Return int
     */
    @Override
    public int saveAuditInfo(AgentActivityDetails agentActivityDetails) {
        return agentActivityDetailsMapper.updateAgentActivityDetails(agentActivityDetails);
    }
}
